﻿using ERPPublic;
using MediatR;
using Newtonsoft.Json;
using SqlSugar;
using WarehouseManagement.Application.Command.OutboundShipmentsCommand;
using WarehouseManagement.Domain.DTO.OutvoundRequests;
using WarehouseManagement.Domain.Entity;
using WarehouseManagement.Infrstructre.Interface;
using static ERPPublic.ParseAPI;

namespace WarehouseManagement.Application.CommandHandler.OutboundShipmentsCommandHandlers
{
    public class OutboundShipmentsSelectCommandHandlers : IRequestHandler<OutboundShipmentsSelectCommand, OutboundRequestSelectDto>
    {
        protected readonly ISqlSugarClient db;
        protected readonly IOutboundRequestRepository outbound;// 出库请求仓储接口
        protected readonly IOutboundRProductRepository outboundR; // 仓库出库申请产品关系仓储接口

        public OutboundShipmentsSelectCommandHandlers(ISqlSugarClient db, IOutboundRequestRepository outbound, IOutboundRProductRepository outboundR)
        {
            this.db = db;
            this.outbound = outbound;
            this.outboundR = outboundR;
        }

        public async Task<OutboundRequestSelectDto> Handle(OutboundShipmentsSelectCommand request, CancellationToken cancellationToken)
        {
            var people = await GetPeopleList(1, 100);
            var list = db.Queryable<OutboundRequest>().LeftJoin<Manpower>((o, p) => o.ManpowerId == p.ManpowerId).Where(o => o.IsDel == false)
                .GroupBy(o => new
                {
                    o.OutboundRequestId,
                    o.ManpowerId,
                    o.OutboundNumber,
                    o.ORapplicationPeople,
                    o.ORapplicationDate,
                    o.OROutType,
                    o.IsDel,
                    o.ORIsState
                }).Select((o, p) => new newOutboundRequestSelectDto
                {
                    MName = p.MName,
                    OutboundRequestId = o.OutboundRequestId,
                    OutboundNumber = o.OutboundNumber,
                    ORapplicationPeople = o.ORapplicationPeople,
                    ORapplicationDate = o.ORapplicationDate,
                    OROutType = o.OROutType,
                    IsDel = o.IsDel,
                    ManpowerId = o.ManpowerId,
                    ORIsState = o.ORIsState
                }).ToList();
            if (request.OROutType != null)
            {
                list = list.Where(x => x.OROutType == request.OROutType).ToList();
            }
            if (request.ORapplicationDatebegin != null)
            {
                list = list.Where(x => Convert.ToDateTime(x.ORapplicationDate) >= Convert.ToDateTime(request.ORapplicationDatebegin)).ToList();
            }
            if (request.ORapplicationDateover != null)
            {
                list = list.Where(x => Convert.ToDateTime(x.ORapplicationDate) < Convert.ToDateTime(request.ORapplicationDateover)).ToList();
            }
            var size = (request.pageindex - 1) * request.pagesize;
            OutboundRequestSelectDto page = new OutboundRequestSelectDto();
            page.pagecount = list.Count();
            page.totalcount = (int)Math.Ceiling(page.pagecount * 1.0 / request.pagesize);
            page.list = list.Skip(size).Take(request.pagesize).ToList();
            return page;
        }

        /// <summary>
        /// 显示人员信息
        /// </summary>
        /// <param name="PName"></param>
        /// <param name="PNumber"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetPeopleList(int pindex, int psize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MCIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTksMTkiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LuY5rSb5ZiJIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0._wh23n1M7asMGST9NyeZj-5rk5ICAiOFv6yDk6e2o7Y";
            HttpClientWrapper cilent = new HttpClientWrapper(token);
            string chanpin = "http://116.205.121.197:8000/vendor/api/Manpower/GetManpowerList?Pindex=" + pindex + "&Psize=" + psize;
            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Manpower>>(postReponse);
            return json.data.list;
        }

        /// <summary>
        /// 显示信息
        /// </summary>
        public class Manpower : Audit
        {
            /// <summary>
            /// 人员档案id
            /// </summary>
            public int ManpowerId { get; set; }
            /// <summary>
            /// 档案编号
            /// </summary>
            public string? MNumber { get; set; }
            /// <summary>
            /// 登录账号
            /// </summary>
            public string? Users { get; set; }
            /// <summary>
            /// 真实姓名
            /// </summary>
            public string? MName { get; set; }
            /// <summary>
            /// 性别
            /// </summary>
            public bool? MSex { get; set; }
            /// <summary>
            /// 出生日期
            /// </summary>
            public DateTime? MBirthDay { get; set; }
            /// <summary>
            /// 身份证号码
            /// </summary>
            public string? MIdCard { get; set; }
            /// <summary>
            /// 联系电话
            /// </summary>
            public string? MPhone { get; set; }
            /// <summary>
            /// 电子邮箱
            /// </summary>
            public string? MEmail { get; set; }
            /// <summary>
            /// 微信号
            /// </summary>
            public string? MWeChat { get; set; }
            /// <summary>
            /// QQ号
            /// </summary>
            public string? MQQ { get; set; }
            /// <summary>
            /// 紧急联系人
            /// </summary>
            public string? MContact { get; set; }
            /// <summary>
            /// 紧急联系人电话
            /// </summary>
            public string? MCPhone { get; set; }
            /// <summary>
            /// 户口地址
            /// </summary>
            public string? MPermanent { get; set; }
            /// <summary>
            /// 家庭住址
            /// </summary>
            public string? MResidence { get; set; }
            /// <summary>
            /// 证件照
            /// </summary>
            public string? MIdPhoto { get; set; }
            /// <summary>
            /// 入职时间
            /// </summary>
            public DateTime? MEntryDate { get; set; }
            /// <summary>
            /// 所在部门
            /// </summary>
            public int? SectorId { get; set; }
            /// <summary>
            /// 所在岗位
            /// </summary>
            public int? PostId { get; set; }

        }
    }
}
